<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Model\Admin\Admin;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use RedisException;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use Util\Common;
use App\Services\Wechat;
use App\Controller\AbstractController;
use App\Model\Admin\Menus;
use App\Model\Admin\Roles;

class MenusController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected Admin $admin;

    #[Inject]
    protected Menus $menus;

    #[Inject]
    protected Roles $roles;


    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }


    /**
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws RedisException
     */
    public function getList(RequestInterface $request): ResponseInterface
    {
        $user = $this->admin->getInfo($request);
        if ($user['roles_id']<= 0) {
            $type ='admin';
            $arr =[];
            $data = $this->menus->where('pid', 0)
                ->where('is_del', Common::DEL_SHOW)
                ->where('status',Common::DEL_SHOW)
                ->orderBy('sort', 'desc')
                ->get(['id', 'name', 'pid', 'url', 'icon', 'component', 'status', 'label', 'sort'])
                ->toArray();
        } else {
            $type = 'editor';
            $roles = Roles::where('id', $user['roles_id'])->value('menus');
            $arr = json_decode($roles, true);
//            ->select(DB::raw('count(*) as user_count, status'))
            $data = $this->menus->where('pid', 0)
                ->where('is_del', Common::DEL_SHOW)
                ->where('status',Common::DEL_SHOW)
                ->whereIn('id', $arr)
                ->orderBy('sort', 'desc')
                ->get(['id', 'name', 'pid', 'url', 'icon', 'component', 'label', 'sort'])
                ->toArray();
        }
        $data = $this->menus->getChild($data,  $type  ,$arr);
        return $this->success($data);
    }

    /**创建编辑菜单
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function createMenus(RequestInterface $request):ResponseInterface
    {
            $data =$request->post();
            if (!array_key_exists('id', $data) || $data['id'] <= 0) {
                Menus::create($data);
            } else {
                if (array_key_exists('children', $data)) {
                    unset($data['children']);
                }
                $this->menus->where('id', $data['id'])->update($data);
            }
            return $this->success($data);

    }

    /**删除菜单
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function delMenus(RequestInterface $request):ResponseInterface
    {
        try {
            $id = $request->input('id');
            $child = Menus::where('pid', $id)
                ->where('is_del', Common::DEL_SHOW)
                ->count();
            if ($child > 0) {
                return $this->error(ErrorCode::MENU_CHILDREN_ERR);
            }
            $this->menus->where('id',$id)->update(['is_del'=>Common::DEL_HIDE]);
            return $this->success();
        } catch (\Exception $e) {
           return $this->error();
        }
    }


}
